package com.hzy.base;

import org.springframework.beans.factory.annotation.Autowired;

public class BaseRepositoryImpl<T> implements BaseRepository<T>{

    private static final String INSERT = "insert";
    private static final String UPDATE = "update";
    private static final String FIND_BY_ID = "findById";
    private static final String DELETE_BY_ID = "deleteById";

    private String sqlMapNameSpace;

    @Autowired
    private PersistenceFactory persistenceFactory;

    protected void setSqlMapNameSpace(String sqlMapNameSpace) {
        this.sqlMapNameSpace = sqlMapNameSpace;
    }

    @Override
    public int update(T model) {
        return persistenceFactory.update(this.sqlMapNameSpace + INSERT, model);
    }

    @Override
    public int insert(T model) {
        return persistenceFactory.insert(this.sqlMapNameSpace + UPDATE, model);
    }

    @Override
    public T findById(Object model) {
        return persistenceFactory.selectOne(this.sqlMapNameSpace + FIND_BY_ID, model);
    }

    @Override
    public int deleteById(T model) {
        return persistenceFactory.delete(this.sqlMapNameSpace + DELETE_BY_ID, model);
    }
}
